// 泛型 声明调用时才知道的类型
// 泛型的语法
function fn<T>(msg:T):T{
    return msg
}
console.log(fn('hello'));
console.log(fn(100));

// 函数泛型
function getMax<T>(arr:T[]):T{
    return  Math.max(...(arr as any)) as T
}
// var arr: number[]=[1,9,7,8,5,3]
console.log(getMax([1,5,6,2,9,7]));

// 类泛型（创建一个能获取指令键值对的对象
class Cls1<K,V>{
    private key:K
    private value:V
    constructor (key:K,value:V){
    this.key = key
    this.value=value
}
getKey():K{
    return this.key
}
getValur():V{
    return this.value
}
}
var c1=new Cls1('name',15)

// 案例：使用class封装一个深拷贝的class
class CloneDeep<T> {
    private value:T
    constructor(newValue:T){
        this.value = JSON.parse(JSON.stringify(newValue))
    }
    getValur():T{
        return this.value
    }
}
var user= {
    name:'zs',
    age:11
}
console.log(new CloneDeep(user).getValur());

type keys = keyof {a:number,b:number}

function getProperty<T,K extends keyof T>(obj:T,key:K):T[K]{
    return obj[key]
}
var person = {name1:'Alice',age:30,email:'alice@aa.com'}
var name1 = getProperty(person,'name1')
var email = getProperty(person,'email')
console.log(name1,email);
